<?php
$this_version = 'SimpleNetwork Alpha Preview 1.0.3';
$this_version_short = 'AP 1.0.3';
$date = mktime();
$start_time = microtime(true);
$upgrading = false;

// Are we being manually run?
if (file_exists(dirname(__FILE__) . '/SSI.php') && !defined('SMF'))
	require_once(dirname(__FILE__) . '/SSI.php');
// No? And no SSI? Uh-oh	
elseif (!defined('SMF'))
	die('<b>Error:</b> Cannot install - please verify you put this file in the same place as SMF\'s SSI.php.');

if (SMF == 'SSI')
	db_extend('packages');

global $modSettings, $smcFunc, $sourcedir, $user_info;


// $modSettings
$mod_settings = array(
	'snet_max_status_length' => 160,
	'snet_max_comment_length' => 160,
	'snet_enable_bbc' => 1,
	'snet_enable_smileys' => 1,
	'snet_enable_like_buttons' => 1,
	'snet_enable' => 1,
	'snet_enable_text' => 1,
	'snet_enable_photo' => 1,	
	'snet_enable_link' => 1,
	'snet_enable_quote' => 1,
);

// If a version is set in the DB, we're upgrading.
if(!empty($modSettings['snet_version']) || !empty($modSettings['snet_installation_date']))
{
	$upgrading = true;
	$old_version = $modSettings['snet_version'];
	$old_version_short = $modSettings['snet_version_short'];
}

// Add all the new tables.
$tables = array();
$tables[] = array(
	'table_name' => '{db_prefix}snet_attachments',
	'columns' => array(
		db_field('id_attach', 'int'),
		db_field('id_status', 'int'),
	),
	'indexes' => array(),
	'if_exists' => 'update',
	'error' => 'fatal',
	'parameters' => array(),
);
$tables[] = array(
	'table_name' => '{db_prefix}snet_comments',
	'columns' => array(
		db_field('id_comment', 'int', 0, true, true),
		db_field('id_status', 'mediumint'),
		db_field('id_user', 'mediumint'),
		db_field('id_note', 'mediumint'),
		db_field('date', 'int'),
		db_field('body', 'text'),
	),
	'indexes' => array(
		array(
			'columns' => array('id_comment'),
			'type' => 'primary',
		),
	),
	'if_exists' => 'update',
	'error' => 'fatal',
	'parameters' => array(),
);
$tables[] = array(
	'table_name' => '{db_prefix}snet_likes',
	'columns' => array(
		db_field('id_like', 'int', 0, true, true),
		db_field('id_status', 'mediumint'),
		db_field('id_comment', 'mediumint'),
		db_field('id_liker', 'mediumint'),
		db_field('id_note', 'mediumint'),
		db_field('level', 'int', 1),
	),
	'indexes' => array(
		array(
			'columns' => array('id_like'),
			'type' => 'primary',
		),
	),
	'if_exists' => 'update',
	'error' => 'fatal',
	'parameters' => array(),
);
$tables[] = array(
	'table_name' => '{db_prefix}snet_status_updates',
	'columns' => array(
		db_field('id_status', 'int', 0, true, true),
		db_field('id_profile', 'mediumint'),
		db_field('id_user', 'mediumint'),
		db_field('type', 'int'),
		db_field('date', 'int'),
		db_field('body', 'text'),		
	),
	'indexes' => array(
		array(
			'columns' => array('id_status'),
			'type' => 'primary',
		),
	),
	'if_exists' => 'update',
	'error' => 'fatal',
	'parameters' => array(),
);
$tables[] = array(
	'table_name' => '{db_prefix}snet_upgrades',
	'columns' => array(
		db_field('id_upgrade', 'mediumint', 0, true, true),
		db_field('from_version', 'varchar'),
		db_field('to_version', 'varchar'),		
		db_field('date', 'int'),	
		db_field('elapsed_time', 'float'),		
		db_field('id_member', 'int'),
	),
	'indexes' => array(
		array(
			'columns' => array('id_upgrade'),
			'type' => 'primary',
		),
	),
	'if_exists' => 'update',
	'error' => 'fatal',
	'parameters' => array(),
);
$tables[] = array(
	'table_name' => '{db_prefix}snet_users',
	'columns' => array(
		db_field('id_user', 'mediumint'),
		db_field('enabled', 'int', 1),
	),
	'indexes' => array(),
	'if_exists' => 'update',
	'error' => 'fatal',
	'parameters' => array(),
);

// Testing data
$rows = array();

// Extra columns
$columns = array();

// Update mod settings
foreach ($mod_settings as $new_setting => $new_value)
{
	if (empty($modSettings[$new_setting]))
		updateSettings(array($new_setting => $new_value));
}

// Feed the hooks!
add_integration_function('integrate_admin_areas','snet_admin_menu');
add_integration_function('integrate_pre_include','Sources/snet/SN-Integrate.php');
add_integration_function('integrate_actions','snet_action_array');
add_integration_function('integrate_pre_load','snet_pre_load');
add_integration_function('integrate_buffer','snet_copyright');

// Create new tables, if any
foreach ($tables as $table)
	$smcFunc['db_create_table']($table['table_name'], $table['columns'], $table['indexes'], $table['parameters'], $table['if_exists'], $table['error']);

// Create new rows, if any
foreach ($rows as $row)
	$smcFunc['db_insert']($row['method'], $row['table_name'], $row['columns'], $row['data'], $row['keys']);

// Create new columns, if any
foreach ($columns as $column)
	$smcFunc['db_add_column']($column['table_name'], $column['column_info'], $column['parameters'], $column['if_exists'], $column['error']);

// Are we done?
if (SMF == 'SSI')
	echo 'Database changes are complete!';

// Add information about this upgrade to smf_settings	
if($upgrading)
{
	updateSettings(array('snet_latest_update_time' => $date));	
	updateSettings(array('snet_latest_update' => $old_version_short . ' > ' . $this_version_short));	
}
else
	updateSettings(array('snet_installation_date' => $date));
	
// Update the version number.
updateSettings(array('snet_version' => $this_version));	
updateSettings(array('snet_version_short' => $this_version_short));		
	
	
// Add info about the installation/upgrade to the log.
$smcFunc['db_insert']('',
	'{db_prefix}snet_upgrades',
	array(
		'from_version' => 'string', 'to_version' => 'string', 'date' => 'int',
		'elapsed_time' => 'float', 'id_member' => 'int',
	),
	array(
		$upgrading ? $old_version : '0', $this_version, $date, (microtime(true) - $start_time), $user_info['id'],
	),			
	array('id_upgrade')
);		

function db_field($name, $type, $size = 0, $unsigned = true, $auto = false)
{
	$fields = array(
		'varchar' => array(
			'auto' => false,
			'type' => 'varchar',
			'size' => $size == 0 ? 50 : $size,
			'null' => false,
		),
		'text' => array(
			'auto' => false,
			'type' => 'text',
			'null' => false,
		),
		'mediumtext' => array(
			'auto' => false,
			'type' => 'mediumtext',
			'null' => false,
		),
		'tinyint' => array(
			'auto' => $auto,
			'type' => 'tinyint',
			'default' => 0,
			'size' => empty($unsigned) ? 4 : 3,
			'unsigned' => $unsigned,
			'null' => false,
		),
		'smallint' => array(
			'auto' => $auto,
			'type' => 'smallint',
			'default' => 0,
			'size' => empty($unsigned) ? 6 : 5,
			'unsigned' => $unsigned,
			'null' => false,
		),
		'mediumint' => array(
			'auto' => $auto,
			'type' => 'mediumint',
			'default' => 0,
			'size' => 8,
			'unsigned' => $unsigned,
			'null' => false,
		),
		'int' => array(
			'auto' => $auto,
			'type' => 'int',
			'default' => 0,
			'size' => empty($unsigned) ? 11 : 10,
			'unsigned' => $unsigned,
			'null' => false,
		),
		'float' => array(
			'auto' => false,
			'type' => 'float',
			'null' => false,
		),		
	);

	$field = $fields[$type];
	$field['name'] = $name;

	return $field;
}
?>